package io.kimmking.rpcfx.netty;

import com.alibaba.fastjson.JSON;
import io.kimmking.rpcfx.api.RpcfxRequest;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil;

/**
 * Create by chenhd on 2021/10/7.
 */
public class MyEncoder extends MessageToByteEncoder<RpcfxRequest> {
    @Override
    protected void encode(ChannelHandlerContext ctx, RpcfxRequest request, ByteBuf out) throws Exception {
        String message = JSON.toJSONString(request);
        out.writeInt(message.getBytes(CharsetUtil.UTF_8).length);
        out.writeBytes(message.getBytes());
    }
}
